package com.qk.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author sch
 * @version 1.0
 * @since 2025-10-08-21:03
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<Role> listRoles(String name, String label, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Role> roles = roleMapper.listRoles(name, label);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public void addRole(Role role) {
        // 设置创建时间和更新时间为当前时间
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }

    @Override
    public Role getRoleById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void updateRole(Role role) {
        // 设置更新时间为当前时间
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public List<Role> listAllRoles() {
        return roleMapper.listAllRoles();
    }
}
